﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Region.Forms
{
    public partial class ProtokolForm : Form, IDBForm
    {
        readonly Engine m_engine;
        readonly Protokol m_protokol;
        readonly ObjectFormUtil m_reader;
        //IDBForm интерфейс
        public ObjectFormUtil i_reader{get{return m_reader;}}
        public DBObject i_object{get{return m_protokol;}}
        public void ShowProtokolLink()
        {
          String textLink = "[Нет]";
          if (m_protokol.RasporID>0)
          {
            Raspor r = m_engine.getRaspor(m_protokol.RasporID);
            textLink = "рас поряжением №" + r.RasporNum + " от " + r.Date.ToShortDateString();
          }
          ProtokolLink.Text = textLink; 
        }
        public void readProtokol()
        {
            this.Text = "Редактирование протокола №" + m_protokol.ProtokolNum.ToString() + " от " + m_protokol.ProtokolReg.ToShortDateString();
            ShowProtokolLink();
            m_reader.get();
        }
        private void saveProtokol()
        {
          if (ExternNameID.SelectedItem == null)
          {
            String newExternName = ExternNameID.Text.Trim();
            foreach(var i in m_engine.ExternName)
              if (i.Value==newExternName)
                ExternNameID.SelectedItem = new Utils.ListItemValue(i.Value,i.Key);
            if (ExternNameID.SelectedItem==null)
            {
              m_engine.addExternName(newExternName);
              foreach(var i in m_engine.ExternName)
                if (i.Value==newExternName)
                {
                  var Item = new Utils.ListItemValue(i.Value,i.Key);
                  ExternNameID.Items.Add(Item);
                  ExternNameID.SelectedItem = Item;
                } 
            }
          }
          if (OspNameID.SelectedItem == null)
          {
            String newOspName = OspNameID.Text.Trim();
            foreach(var i in m_engine.OspName)
              if (i.Value==newOspName)
                OspNameID.SelectedItem = new Utils.ListItemValue(i.Value,i.Key);
            if (OspNameID.SelectedItem==null)
            {
              m_engine.addOspName(newOspName);
              foreach(var i in m_engine.OspName)
                if (i.Value==newOspName)
                {
                  var Item = new Utils.ListItemValue(i.Value,i.Key);
                  OspNameID.Items.Add(Item);
                  OspNameID.SelectedItem = Item;
                } 
            }
          }
          m_reader.set();
          m_protokol.save();      
        }
        public ProtokolForm(Engine engine,Int32 ProtokolID)
        {
            m_engine = engine;
            m_protokol = m_engine.getProtokol(ProtokolID);
            m_reader = new ObjectFormUtil(m_engine, m_protokol);
            InitializeComponent();
            Dictionary<Int32,String> IspUsers = m_engine.UserFIO;
            IspUsers[0] = "[Не указан]";
            var ListExProcessor = new Dictionary<String,Object>()
            {
              {"ProtokolIspUserID",IspUsers},
              {"ArticleID", m_engine.Article},
              {"FromTypeID",m_engine.FromType},
              {"ExternNameID",m_engine.ExternName},
              {"ProtokolKontTypeID",m_engine.KontType},
              {"ProtokolCheckType",m_engine.CheckType},
              {"MeasureID",m_engine.Measure},
              {"MeasureSudID",m_engine.MeasureSud},
              {"OspNameID",m_engine.OspName},
              {"ProtokolForm112",m_engine.Form112forTree}
            };
            //m_protokol.OS
            DBForm.addEditors(this,this,ListExProcessor);
            /*m_reader.AddEditor(ProtokolReg, "ProtokolReg");
            m_reader.AddEditor(ProtokolNum, "ProtokolNum");
            m_reader.AddEditor(ProtokolNumExtern, "ProtokolNumExtern");
            m_reader.AddEditor(ProtokolCreate, "ProtokolCreate");
            m_reader.AddEditor(ProtokolIspUserID, "ProtokolIspUserID", m_engine.UserFIO);
            m_reader.AddEditor(ProtokolComment, "ProtokolComment");
            m_reader.AddEditor(ArticleID, "ArticleID",m_engine.Article);*/    
            //Font = new Font(Font.FontFamily, (float)ps.RasporForm_Size);
            readProtokol();
        }

        private void label13_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
        
        void SaveButtonClick(object sender, EventArgs e)
        {
			   saveProtokol();       	
        }

        
        void Button1Click(object sender, EventArgs e)
        {
          saveProtokol();
        }
        
        void Label4Click(object sender, EventArgs e)
        {
        	
        }
        
        void Button2Click(object sender, EventArgs e)
        {
              DirItem root = new DirItem(); 
              root.setData(0, "Caption", "Связать протокол");
              var Wheres = new Dictionary<String,Object>();
              Wheres["RasporUserID"] = m_protokol.ProtokolIspUserID;
              Wheres["ParentOtdelID"] = 18;
              root.Add(new DirReestrRaspor(m_engine,"Распоряжения","",Wheres));
              var form = new BrowserForm(m_engine, root, true);
              Int32 RasporID = 0;
              form.isSelectItem = (item)=>
              { 
                if(item==null)
                  return false; 
                return (item.getData(0,"Type")=="Raspor");
              };
              form.SelectItem = (item)=>              
              {
                if (item.getData(0,"Type")=="Raspor") 
                  RasporID = (Int32)item.getData(0,"ID");
                else 
                  RasporID = 0;
              };
              form.ShowDialog();
              m_protokol.RasporID = RasporID;
              ShowProtokolLink();
        }
    }
}
